<template>
    <div class="layout-content">
        <div class="layout-content-main">
            <div class="content-div">
                <Row style="margin-bottom:5px;">
                    <Col span="19" class="table-form-input-select-col">
                    <span class="alltitle">项目列表</span>
                    </Col>
                    <!--  <Col span="3" class="table-form-input-select-col" order="4">
                   <Button type="primary" icon="ios-book" size="large" @click="addData">新增项目</Button>
                   </Col>
                   <Col span="2" class="table-form-input-select-col" order="4">
                   <Button type="error" icon="ios-trash" size="large" @click="deleteall">删除</Button>
                   </Col> -->
                </Row>
            </div>
            <!--    <hr class="hr1" /> -->
            <div class="content-div">
                <Table border :columns="columns2" :data="data" ref="table" @on-selection-change="selectChange"></Table>
            </div>
        </div>
        <!-- <Modal v-model="addDataModalFlag" title="新增项目" @on-ok="handleSubmit('formValidate')" :mask-closable="false"> -->
            <!-- <Form :label-width="100" ref="formValidate" :rules="ruleValidate" :model="formValidate"> -->
                <!-- <FormItem label="项目编号" prop="code"> -->
                    <!-- <Input placeholder="输入项目编号" class="input-select-class" v-model="formValidate.code"></Input> -->
                <!-- </FormItem> -->
                <!-- <FormItem label="项目名称" prop="name"> -->
                    <!-- <Input placeholder="输入项目名称" class="input-select-class" v-model="formValidate.name"></Input> -->
                <!-- </FormItem> -->
                <!-- <FormItem label="年份" prop="year"> -->
                    <!-- <Select class="input-select-class" value="2019" v-model="formValidate.year"> -->
                        <!-- <Option value="2019">2019</Option> -->
                        <!-- <Option value="2020">2020</Option> -->
                        <!-- <Option value="2021">2021</Option> -->
                        <!-- <Option value="2022">2022</Option> -->
                        <!-- <Option value="2023">2023</Option> -->
                        <!-- <Option value="2024">2024</Option> -->
                        <!-- <Option value="2025">2025</Option> -->
                        <!-- <Option value="2026">2026</Option> -->
                        <!-- <Option value="2027">2027</Option> -->
                    <!-- </Select> -->
                <!-- </FormItem> -->
                <!-- <FormItem label="执行人" prop="operate_name"> -->
                    <!-- <Input placeholder="输入执行人" class="input-select-class" v-model="formValidate.operate_name" @on-blur="douser"></Input> -->
                <!-- </FormItem> -->
                <!-- <FormItem label="主管" prop="supervisor_name"> -->
                    <!-- <Select class="input-select-class" value="2019" v-model="formValidate.supervisor_name"> -->
                        <!-- <Option value="2019">天才</Option> -->
                    <!-- </Select> -->
                <!-- </FormItem> -->
            <!-- </Form> -->
        <!-- </Modal> -->
        <!-- <Modal v-model="showDataModalFlag" title="修改项目" @on-ok="handleSubmit('formValidate')" :mask-closable="false"> -->
            <!-- <Form :label-width="100" ref="formValidate" :rules="ruleValidate" :model="showDataModalObject"> -->
                <!-- <FormItem label="项目编号" prop="code"> -->
                    <!-- <Input placeholder="输入项目编号" class="input-select-class" v-model="showDataModalObject.code"></Input> -->
                <!-- </FormItem> -->
                <!-- <FormItem label="项目名称" prop="name"> -->
                    <!-- <Input placeholder="输入项目名称" class="input-select-class" v-model="showDataModalObject.name"></Input> -->
                <!-- </FormItem> -->
                <!-- <FormItem label="年份" prop="year"> -->
                    <!-- <Select class="input-select-class" v-model="showDataModalObject.year"> -->
                        <!-- <Option value="2019">2019</Option> -->
                        <!-- <Option value="2020">2020</Option> -->
                        <!-- <Option value="2021">2021</Option> -->
                        <!-- <Option value="2022">2022</Option> -->
                        <!-- <Option value="2023">2023</Option> -->
                        <!-- <Option value="2024">2024</Option> -->
                        <!-- <Option value="2025">2025</Option> -->
                        <!-- <Option value="2026">2026</Option> -->
                        <!-- <Option value="2027">2027</Option> -->
                    <!-- </Select> -->
                <!-- </FormItem> -->
                <!-- <FormItem label="执行人" prop="operate_name"> -->
                    <!-- <Input placeholder="输入执行人" class="input-select-class" v-model="showDataModalObject.operate_name"></Input> -->
                <!-- </FormItem> -->
                <!-- <FormItem label="主管" prop="supervisor_name"> -->
                    <!-- <Select class="input-select-class" v-model="showDataModalObject.supervisor_name"> -->
                        <!-- <Option :value="showDataModalObject.supervisor_name">{{showDataModalObject.supervisor_name}}</Option> -->
                    <!-- </Select> -->
                <!-- </FormItem> -->
            <!-- </Form> -->
        <!-- </Modal> -->
    </div>
</template>
<script>
export default {

    data() {
            return {
                formValidate: {
                    code: '',
                    name: '',
                    year: '',
                    operate_name: '',
                    supervisor_name: ''

                },
                ruleValidate: {
                    name: [{
                        required: true,
                        message: '项目名不能为空',
                        trigger: 'blur'
                    }],
                    code: [{
                        required: true,
                        message: '项目编号不能为空',
                        trigger: 'blur'
                    }],
                    year: [{
                        required: true,
                        message: '年份不能为空',
                        trigger: 'blur'
                    }],
                    operate_name: [{
                        required: true,
                        message: '操作人不能为空',
                        trigger: 'blur'
                    }],


                },
                showDataModalObject: {},
                addDataModalObject: {},
                showDataModalFlag: false,
                addDataModalFlag: false,

                // columns2: [{title: '序号',key: 'id',width: 80,sortType: "asc"

                columns2: [{
                        title: '序号',
                        key: 'id',
                        width: 80,
                    }, {
                        title: '项目编号',
                        key: 'code',

                    }, {
                        title: '项目名称',
                        key: 'name',

                    }, {
                        title: '年份',
                        key: 'year',
                        width: 100
                    }, {
                        title: '执行人',
                        key: 'operate_name',
                        width: 140
                    }, {
                        title: '主管',
                        key: 'supervisor_name',
                        width: 140
                    }, {
                        title: '类型',
                        key: 'ptype_r_q',
                        width: 200
                    },{
                        title: '操作',
                        key: 'action',
                        render: (h, params) => {
                            return h('div', [
                                h('Button', {
                                    props: {
                                        type: 'info',
                                        size: 'small'
                                    },
                                    style: {
                                        marginRight: '5px'
                                    },
                                    on: {
                                        click: () => {
                                            this.showData(params.index)
                                        }
                                    }
                                }, '后台管理'),
                            ])
                        }
                    }

                ],
                data: [],
                pageData: {
                    current: 1,
                    total: 0,
                    pageSize: 20
                }
            }
        },
        created() {
            var self = this
            $.ajax({
                url: "https://evp.51job.com/51job/api/51miniadmin/back.php?action=projectlist",
                type: "POST",
                dataType: "json",
                success: function(data) {

                    console.log(data);

                    if (data.data != null) {

                        self.data = data.data
                        self.pageData.total = parseInt(data.datatotal)
                        self.pageData.pageSize = 20
                        self.current = 1

                    } else {

                        self.$Message.error('没有项目');

                    }

                }
            });
        },
        methods: {
            changepage(currentPage) {
                if (isNaN(currentPage)) {
                    currentPage = 1
                }
                var self = this
                    /*self.$api.get('/api/v1/table_data', null, function (result) {
                      self.data = result.resultData.dataList
                      self.pageData = result.resultData.pageData
                    })*/
                var searchdata = {};

                console.log(searchdata)

                $.ajax({
                    url: "https://evp.51job.com/51job/api/51miniadmin/back.php?action=projectlist",
                    type: "POST",
                    dataType: "json",
                    success: function(data) {

                        console.log(data);

                        if (data.data != null) {

                            self.data = data.data
                            self.pageData.total = parseInt(data.datatotal)
                            self.pageData.pageSize = 20
                            self.current = currentPage

                        } else {

                            self.$Message.error('没有项目');

                        }

                    }
                });
            },
            douser() {
                console.log(this.formValidate.operate_name)

                $.ajax({
                    url: "https://evp.51job.com/51job/api/51miniadmin/back.php?action=snamefromuname",
                    data: {
                        "name": this.formValidate.operate_name,
                    },
                    type: "POST",
                    dataType: "json",
                    success: function(data) {

                        console.log(data);


                    }
                });

            },
            showData(index) {
                var self = this
                self.showDataModalFlag = true
                    //self.showDataModalObject = self.data[index]
                self.$router.push({
                    name: '公司信息',
                    params: {
                        id: self.data[index].id,
                    }

                })
                // 这个什么身份？
                var uid = localStorage.uid
                var authorize = localStorage.authorize
                if (authorize == '0' || authorize == '1' || authorize == '2') {
                    localStorage.ptype=self.data[index].type
                    localStorage.preservestaff=self.data[index].reservestaff
                    localStorage.pquestionnaire=self.data[index].questionnaire
                } else if (authorize == '3') {
                    if (uid == self.data[index].teamwork1_id) {
                      localStorage.ptype=self.data[index].type1
                      localStorage.preservestaff=self.data[index].reservestaff1
                      localStorage.pquestionnaire=self.data[index].questionnaire1
                    } else if (uid == self.data[index].teamwork2_id) {
                      localStorage.ptype=self.data[index].type2
                      localStorage.preservestaff=self.data[index].reservestaff2
                      localStorage.pquestionnaire=self.data[index].questionnaire2
                    }
                }
                
                localStorage.pid=self.data[index].id
                localStorage.pcode=self.data[index].code
                localStorage.pname=self.data[index].name
            },
            addData(index) {
                var self = this
                self.addDataModalFlag = true
                self.addDataModalObject = self.data[index]
            },
            changeDate(dateA) {

                var dateee = new Date(dateA).toJSON()

                var date = new Date(+new Date(dateee) + 8 * 3600 * 1000).toISOString().replace(/T/g, ' ').replace(/\.[\d]{3}Z/, '')

                return date
            },
            handleSubmit(name) {
                this.$refs[name].validate((valid) => {
                    if (valid) {
                        var self = this
                        console.log(self.changeDate(self.formValidate.sdate))

                        $.ajax({
                            url: "http://evp.51job.com/51job/api/51jobtrain/back_api.php?action=addad",
                            data: {
                                "postion": self.formValidate.postion,
                                "coname": self.formValidate.coname,
                                "sdate": self.changeDate(self.formValidate.sdate),
                                "edate": self.changeDate(self.formValidate.edate),
                                "sort": self.formValidate.sort,
                            },
                            type: "POST",
                            dataType: "json",
                            success: function(data) {

                                console.log(data);
                                self.$router.push({
                                    path: '/广告设置'
                                })
                                self.$Message.success('提交成功!')
                                self.changepage(1);

                            }
                        });


                    } else {
                        this.$Message.error('表单验证失败!')
                    }
                })

            },



            removeData(index) {
                var self = this
                self.$api.post('/api/v1/edit', self.data[index], function(result) {
                    self.data.splice(index, 1)
                    self.$Message.info(result.message)
                })
            },
            exportData(type) {

                this.$refs.table.exportCsv({
                    filename: 'The original data'
                });

            },
            selectChange(selection, index) {
                this.multipleSelection = selection;

            },
            deleteall() {

                //console.log(this.multipleSelection);
                if (this.multipleSelection == null) {
                    this.$Message.error('请至少勾选一项');
                } else {

                    this.$Modal.confirm({
                        title: '确认删除？',
                        content: '',
                        onOk: () => {
                            var arr = this.multipleSelection;
                            console.log(arr)
                            var self = this;
                            self.$api.post('/api/v1/edit', self.data, function(result) {

                                let add = self.data.filter(item => !arr.some(ele => ele.id === item.id));
                                self.data = add;

                                var jobarr = [];
                                for (var i = 0; i < arr.length; i++) {

                                    jobarr.push(arr[i].id);
                                }
                                console.log(jobarr)
                                $.ajax({
                                    url: "http://evp.51job.com/51job/api/51jobtrain/back_api.php?action=delad",
                                    data: {
                                        "id[]": jobarr
                                    },
                                    type: "POST",
                                    dataType: "json",
                                    success: function(data) {

                                        console.log(data);
                                        self.changepage(1);

                                    }
                                });

                            })
                        },
                        onCancel: () => {
                            this.$Message.info('取消删除');
                        }
                    });




                }
            },
        }
}
</script>
